﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;

namespace DemoData.Model
{
    public class Order : ModelBase
    {
        private Product m_Product;
        private Customer m_Customer;

        [RangeValidator(1, RangeBoundaryType.Inclusive, int.MaxValue, RangeBoundaryType.Inclusive)]
        public int ProductId { get; set; }

        [RangeValidator(1, RangeBoundaryType.Inclusive, int.MaxValue, RangeBoundaryType.Inclusive)]
        public int CustomerId { get; set; }

        public Product Product
        {
            get
            {
                if (m_Product == null || m_Product.Id != this.ProductId)
                {
                    m_Product = Repository.Products.Get().FirstOrDefault(p => p.Id == this.ProductId);
                }

                return m_Product;
            }
        }

        public Customer Customer
        {
            get
            {
                if (m_Customer == null || m_Customer.Id != this.CustomerId)
                {
                    m_Customer = Repository.Customers.Get().FirstOrDefault(c => c.Id == this.CustomerId);
                }

                return m_Customer;
            }
        }

        public int Quantity { get; set; }

        public decimal UnitPrice { get; set; }

        public decimal UnitPriceDiscount { get; set; }

        public decimal LineTotal { get; set; }

        public Order()
        {
        }

        public Order(int quantity, int productId, decimal unitPrice, decimal unitPriceDiscount, decimal lineTotal, int customerId)
        {
            this.Quantity = quantity;
            this.ProductId = productId;
            this.UnitPrice = unitPrice;
            this.UnitPriceDiscount = unitPriceDiscount;
            this.LineTotal = lineTotal;
            this.CustomerId = customerId;
        }
    }
}
